package org.zhenglide.oa.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zhenglide.oa.entity.Employee;
import org.zhenglide.oa.service.IEmployeeService;
@Component
public class AssigneeListener implements TaskListener {
    
//	@Autowired
//	IEmployeeService empService;
	
	@Override
	public void notify(DelegateTask task) {
		// TODO Auto-generated method stub
		/**
		 * 
		 *  设置任务代理人
		 */
		 ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
	        if(null != requestAttributes) {
	            HttpServletRequest request = requestAttributes.getRequest();
	            HttpSession session = request.getSession();
	            Employee employee = (Employee)session.getAttribute("employee");
	        	WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
	    		IEmployeeService employeeService = (IEmployeeService) ac.getBean("employeeService");
	            Employee manager = employeeService.getById(employee.getManagerId());
	            task.setAssignee(manager.getName());
	            
	        }
		
		
	}
	
	
//	public void queryEmp(){
//		Employee manager = empService.getById(1);
//		System.out.println(manager.getName());
//	}

}
